<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      v-show="showSearch"
      label-width="0px"
    >
    <el-row :gutter="20">
      <el-col :span="6">
        <el-form-item label="" prop="serialCode">
        <el-input
          v-model="queryParams.serialCode"
          placeholder="申请编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      </el-col>
      <el-col :span="6">
          <el-form-item label="" prop="orgName">
            <el-input
              v-model="queryParams.orgName"
              placeholder="企业名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="" prop="projectName">
          <el-select v-model="queryParams.projectName" filterable multiple collapse-tags size="small" placeholder="应用项目" style="width:100%" clearable>
              <el-option
                v-for="(item,index) in SelectAppName"
                :key="index"
                :label="item.projectName"
                :value="item.projectName"
              />
            </el-select>
      </el-form-item>
      </el-col>
      <el-col :span="6">
          <el-form-item label="" prop="department">
            <el-select v-model="queryParams.department" size="small" placeholder="所属科室" style="width:100%" clearable>
              <el-option
                v-for="item in deptOptions"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptName">
              </el-option>
            </el-select>
          </el-form-item>
      </el-col>
      <el-col :span="6">
          <el-form-item label="" prop="approvalStatus">
            <el-select v-model="queryParams.approvalStatus" size="small" placeholder="当前状态" style="width:100%" clearable>
              <el-option
                v-for="(item,index) in SelectStatus"
                :key="index"
                :label="item.approvalStatus"
                :value="item.approvalStatus"
              />
            </el-select>
          </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="">
          <el-date-picker
          size="small"
            v-model="dateRange"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="申请开始日期"
            end-placeholder="申请结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="">
          <el-date-picker
          size="small"
            v-model="dateRanges"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="受理开始日期"
            end-placeholder="受理结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="">
          <el-date-picker
          size="small"
            v-model="createTime"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="创建开始日期"
            end-placeholder="创建结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          :disabled='multiple'
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="todoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" fixed type="index" width="55" align="center" />
      <el-table-column label="申请编号" fixed align="center" prop="serialCode" width="140">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleProcess(scope.row)">{{scope.row.serialCode}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="企业名称" fixed prop="orgName" align="center" width="150"  :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column label="应用项目" align="center" prop="projectName" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column label="所属科室/系统" align="center" prop="department">
      </el-table-column>
       <el-table-column label="当前状态" align="center" prop="approvalStatus" width="200">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_status" :value="scope.row.approvalStatus"/>
        </template> -->
      </el-table-column>
      <el-table-column label="申请日期" align="center" sortable prop="applyForTime" width="150" />
      <el-table-column label="受理日期" align="center" sortable prop="previousNodeDate" width="150" />
      <el-table-column label="创建日期" align="center" sortable prop="createTime" width="150" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <!-- <el-button
            v-if="scope.row.approvalStatus == 1 || scope.row.approvalStatus == 4"
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
          >受理</el-button>
          <el-button
            v-else-if="scope.row.approvalStatus == 2 || scope.row.approvalStatus == 3 || scope.row.approvalStatus == 5 || scope.row.approvalStatus == 6"
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
          >审核</el-button>
          <el-button
            v-else-if="scope.row.approvalStatus == 7 || scope.row.approvalStatus == 8 || scope.row.approvalStatus == 9"
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
          >处理</el-button>
          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
          >办理</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
          >办理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { mergeTodoList,mytodoSelectStatus,mytodoSelectAppName } from "@/api/businessModule/merge.js";
import { deptListAll } from "@/api/administeration/askForLeave.js";
export default {
 name: "mergeTodo",
  dicts:['sys_project_name'],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 状态数组
      SelectStatus: [],
      // SelectAppName下拉
      SelectAppName: [],
      // 所属科室
      deptOptions:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程待办任务表格数据
      todoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      dateRanges: [],
      createTime:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: [],
        orgName:null,
        approvalStatus: null,
        serialCode: null,
        department:null,
      },
      procName:null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      Code:'CODE_YW',
    };
  },
  // 监听路由回来的地址进行刷新数据
  // watch: {
  //   $route(newVal, oldVal) {
  //     if (newVal.path == '/businessModule/merge') {
  //       this.getList()
  //     }

  //   }
  // },
   // 利用activated函数来进行函数调用，这样可以保证每次进入界面都能刷新界面内容
  activated() {
    this.getList()
  },
  created() {
    this.getList()
  },
  
  // mounted() {
  //   window.console.log(this.$router);
  // },
  methods: {
    // 项目名称下拉
    async getSelectAppName() {
      const resName = await mytodoSelectAppName(this.Code)
      this.SelectAppName = resName.data
    },
    /** 状态下拉数据查询 */ 
    async getSelectStatus() {
      const res = await mytodoSelectStatus(this.Code)
      this.SelectStatus = res.data
    },
    // 下拉部门
    async getDeptListAll() {
      const deptres = await deptListAll()
      this.deptOptions = deptres.data
    },
    /** 查询流程定义列表 */
    getList() {
      this.loading = true;
      let obj = {}
      if (this.dateRange != null && this.dateRange.length>0 ) {
        obj = {
          applyForStartDate:this.dateRange[0],
         applyForEndDate:this.dateRange[1]
        }
      }
      let objNew = {}
      if (this.dateRanges != null && this.dateRanges.length>0 ) {
        objNew = {
          previousNodeStartDate:this.dateRanges[0],
         previousNodeEndDate:this.dateRanges[1]
        }
      }
      let createObj = {}
      if (this.createTime != null && this.createTime.length>0 ) {
        createObj = {
          createStartDate:this.createTime[0],
         createEndDate:this.createTime[1]
        }
      }
      
      mergeTodoList({ ...this.queryParams,projectName:this.queryParams.projectName.toString(),...obj,...objNew,...createObj },this.Code).then(
        response => {
          this.todoList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
      this.getDeptListAll()
      this.getSelectStatus()
      this.getSelectAppName()
    },
    // 跳转到处理页面
    // handleProcess(row) {
    //   console.log(row);
    //   this.$router.push({
    //     path: "/workflow/process/detail/" + row.procInsId,
    //     query: {
    //       taskId: row.taskId,
    //       definitionId: row.procDefId,
    //       procInsId: row.procInsId,
    //       deployId: row.deployId,
    //       finished: true,
    //       processed: true
    //     }
    //   });
    // },
    handleProcess(row) {
      this.$router.push({
        path: "/workflow/process/mergedetail/" + row.procInstId,
        query: {
          taskId: row.taskId,
          processed: true
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        category: null,
        key: null,
        tenantId: null,
        deployTime: null,
        derivedFrom: null,
        derivedFromRoot: null,
        parentDeploymentId: null,
        engineVersion: null
      };
      this.resetFormData("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.dateRanges = [];
      this.createTime = []
      this.resetFormData("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增流程定义";
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "workflow/process/todoExport",
        {
          ...this.queryParams
        },
        `wf_todo_process_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
